package com.bing.bingPro.service.impl;

import com.bing.bingPro.entity.DbConnectionInfo;
import com.bing.bingPro.service.DatabaseConnector;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
@Service
public class MySqlDatabaseConnector implements DatabaseConnector {
    @Override
    public boolean testConnection(DbConnectionInfo connectionInfo) {
        String url = "jdbc:mysql://" + connectionInfo.getHost() + ":" + connectionInfo.getPort() + "/" + connectionInfo.getDatabaseName();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, connectionInfo.getUsername(), connectionInfo.getPassword());
            // 测试连接是否成功
            if (connection != null) {
                connection.close();
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
